//////////////////////////////////////////////////
// STUDENT RESEARCH PROJECT						//
// UNIVERSITY OF HAGEN 2012						//
// File:	DataContainer.cpp					//
// Date:	01.09.2012							//
// Author:	Max Pejs							//
//////////////////////////////////////////////////

#include "DataContainer.h"

// Initialize singleton instance with NULL
DataContainer* DataContainer::owner = NULL;

DataContainer::DataContainer()
{
	begin = 0;
	end = 0;
	size = RING_BUFFER_SIZE + 1;
}

DataContainer * DataContainer::GetInstance(void)
{
	if(owner == NULL)
	{
		owner = new DataContainer();
	}
	return owner;
}

UInt8 DataContainer::ringBufferIsFull()
{
	return ((end+1) % size) == begin; 
}

UInt8 DataContainer::ringBufferIsEmpty(void)
{
	return end == begin;
}

UInt8 DataContainer::addBit(UInt8 bit)
{
	if(ringBufferIsFull())
	{
		return 0;
	}
	ringBuffer[end] = bit;
	end = (end + 1) % size;
	return 1;
}

UInt8 DataContainer::getBit(void)
{
	UInt8 ret = 0xFF;
	if(!ringBufferIsEmpty())
	{
		ret = ringBuffer[begin];
		begin = (begin+1) % size;
	}

	return ret;
}

UInt8 DataContainer::getSize(void)
{
	if(end >= begin)
	{
		return end - begin;
	}
	else
	{
		return  (end + size) - begin ;
	}
}